package com.xxxx.server.utils;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.UUID;

/**
 * @description: 文件工具类
 * @author: Honors
 * @create: 2021-11-10
 */
public class FileUtils {

    private static final String BASEDIRNAME = "resource";
    private static final String WEBSEPARATOR = "/";

    /**
     * 获取永久目录绝对路径
     *
     * @return
     */
    public static String getAbsPath() {
        return System.getProperty("user.dir") + File.separator + "yeb-server" +
                File.separator + "src" + File.separator + "main" +
                File.separator + "resources" + File.separator + "file" + File.separator;
    }

    /**
     * 获取临时目录绝对路径
     *
     * @return
     */
    public static String getTempAbsPath() {
        return System.getProperty("user.dir") + File.separator
                + "yeb-server" + File.separator + "src"
                + File.separator + "main" + File.separator +
                "resources" + File.separator + "temp" + File.separator;
    }

    /**
     * 获取访问路径
     *
     * @param fileName
     * @return
     */
    public static String getFilePath(String fileName) {
        StringJoiner joiner = new StringJoiner(WEBSEPARATOR, WEBSEPARATOR, WEBSEPARATOR);
        joiner.add(BASEDIRNAME);
        joiner.add("file");
        return joiner.toString() + fileName;
    }

    /**
     * 文件上传-临时目录
     *
     * @param file
     */
    public static String uploadFileTemp(MultipartFile file) throws IOException {
        //获取文件名
        String filePath = "";
        if (Objects.nonNull(file)) {
            //获取文件名
            String fileName = file.getOriginalFilename();
            //获取文件后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //重新生成文件名
            filePath = FileUtils.getTempAbsPath() + System.currentTimeMillis() + suffixName;
            file.transferTo(new File(filePath));
        } else {
            throw new AccessDeniedException("上传文件为空!");
        }
        return filePath;
    }

    /**
     * 文件上传-永久目录
     *
     * @param file
     */
    public static String uploadFileAbs(MultipartFile file) throws IOException {
        //获取文件名
        String filePath = "";
        if (Objects.nonNull(file)) {
            //获取文件名
            String fileName = file.getOriginalFilename();
            //获取文件后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //重新生成文件名
            filePath = FileUtils.getAbsPath() + System.currentTimeMillis() + suffixName;
            file.transferTo(new File(filePath));
        } else {
            throw new AccessDeniedException("上传文件为空!");
        }
        return filePath;
    }

    public static void deleteFile(String filePath) {
        // 删除附件
        File file = new File(filePath);
        // 文件是否存在
        boolean isOK = file.exists();
        // 存在就删除
        if (isOK) {
            file.delete();
        }
    }

    public static void fileCopy(String filePath, String path) {
        FileInputStream fr = null;   // 字符输入流
        FileOutputStream fw = null;   // 字符输出流
        try {
            fr = new FileInputStream(filePath);
            fw = new FileOutputStream(path);
            byte[] c = new byte[1024];  //  字符类char
            int len = 0;
            while ((len = fr.read(c)) != -1) {
                fw.write(c, 0, len);
                fw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fr != null) {
                        try {
                            fr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    /**
     * 32位默认长度的uuid
     * (获取32位uuid)
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * (获取指定长度uuid)
     *
     * @return
     */
    public static String getUUID(int len) {
        if (0 >= len) {
            return null;
        }
        String uuid = getUUID();
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < len; i++) {
            str.append(uuid.charAt(i));
        }
        return str.toString();
    }


}
